package com.qsmx.qingqiuticket.app.delegate.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qsmx.qingqiuticket.app.R;
import com.qsmx.qingqiuticket.app.delegate.holder.CouponHolder;
import com.qsmx.qingqiuticket.app.entity.CouponEntity;

import java.util.List;

public class CouponAdapter extends RecyclerView.Adapter<CouponHolder> {

    private Context mContext;
    private List<CouponEntity.BodyBean> mData;

    public CouponAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<CouponEntity.BodyBean> data) {
        mData = data;
    }

    @NonNull
    @Override
    public CouponHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CouponHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_coupon, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CouponHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvCouponName.setText(mData.get(position).getCouponOuterName());
        holder.tvCouponMoney.setText(String.valueOf(mData.get(position).getPrice()));
        holder.tvCouponUseMoney.setText(String.format(mContext.getString(R.string.coupon_use_money), String.valueOf(mData.get(position).getUseThresholdPrice())));
        holder.tvCouponUseTime.setText(mData.get(position).getBeginTime() + "-" + mData.get(position).getEndTime());
        if (mData.get(position).getUseRange() == 1) {
            holder.tvCouponTips.setText("门票、充能使用");
        } else if (mData.get(position).getUseRange() == 2) {
            holder.tvCouponTips.setText("门店核销");
        } else if (mData.get(position).getUseRange() == 3) {
            holder.tvCouponTips.setText("实物商品使用");
        } else if (mData.get(position).getUseRange() == 4) {
            holder.tvCouponTips.setText("全品类通用");
        }

        if (mData.get(position).isSelect()) {
            holder.cbCouponSelect.setChecked(true);
        } else {
            holder.cbCouponSelect.setChecked(false);
        }

        holder.clContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (CouponEntity.BodyBean data : mData) {
                    data.setSelect(false);
                }
                mData.get(position).setSelect(true);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mData != null && mData.size() > 0) {
            return mData.size();
        } else {
            return 0;
        }
    }
}
